<?php

namespace app\admin\model;

use think\Model;

class Admin extends Model
{

    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $hidden = [
        'password',
        'salt',
    ];
    protected $append = [
        'year_month',
        'year_month_str',
        'total_assembly',
    ];

    public static function init()
    {
        self::beforeWrite(function ($row) {
            $changed = $row->getChangedData();
            //如果修改了用户或或密码则需要重新登录
            if (isset($changed['username']) || isset($changed['password']) || isset($changed['salt'])) {
                $row->token = '';
            }
        });
    }

    public function getYearMonthAttr($value)
    {
        $year_month = request()->param('year_month');
        $year_month = $year_month ? $year_month : date("Y-m");
        return strtotime($year_month);
    }

    public function getYearMonth($year_month = '')
    {
        $year_month = $year_month ? $year_month : request()->param('year_month');
        return strtotime($year_month ? $year_month : date("Y-m"));
    }

    public function getYearMonthStrAttr($value)
    {
        return date("Y年m月", $this->getYearMonth());
    }

    public function getTotalAssemblyAttr($value, $data)
    {
        $currTime = strtotime(date('Y-m', $this->getYearMonth()) . date('-d H:i:s'));
        $firstDay = date('Y-m-01', $this->getYearMonth());
        $lastDay = strtotime("$firstDay +1 month -1 day");

        $package_ids = (new \app\admin\model\customer\package\User())->where(['admin_id' => $data['id']])->column('package_id');

        $commission = 0;

        //会计
        if ($data['identity'] == 1) {
            //收款金额*%
            $where = [
                'create_time' => ['between', [strtotime($firstDay), $lastDay]],
                'package_id' => ['in', $package_ids],
                'account_commission_type' => ['=', 0],
            ];
            $commission += (new \app\admin\model\customer\Service())
                ->where($where)
                ->sum('account_commission');

            //固定金额
            $where = [
                'create_time' => ['between', [strtotime($firstDay), $lastDay]],
                'package_id' => ['in', $package_ids],
                'account_commission_type' => ['=', 1],
            ];
            $commission += (new \app\admin\model\customer\Service())
                ->where($where)
                ->sum('account_commission');
            //每月提成
            $where = [
                'start_time' => ['<=', $currTime],
                'end_time' => ['>=', $currTime],
                'package_id' => ['in', $package_ids],
                'account_commission_type' => ['=', 2],
            ];
            $commission += (new \app\admin\model\customer\Service())
                ->where($where)
                ->sum('account_commission');
        }

        //销售
        if ($data['identity'] == 0) {
            $package_ids = (new \app\admin\model\customer\Package)->where(['admin_id' => $data['id']])->column('package_id');

            //收款金额*%
            $where = [
                'create_time' => ['between', [strtotime($firstDay), $lastDay]],
                'package_id' => ['in', $package_ids],
                'sales_commission_type' => ['=', 0],
            ];
            $commission += (new \app\admin\model\customer\Service())
                ->where($where)
                ->sum('sales_commission');

            //固定金额
            $where = [
                'create_time' => ['between', [strtotime($firstDay), $lastDay]],
                'package_id' => ['in', $package_ids],
                'sales_commission_type' => ['=', 1],
            ];
            $commission += (new \app\admin\model\customer\Service())
                ->where($where)
                ->sum('sales_commission');
            //每月提成
            $where = [
                'start_time' => ['<=', $currTime],
                'end_time' => ['>=', $currTime],
                'package_id' => ['in', $package_ids],
                'sales_commission_type' => ['=', 2],
            ];
            $commission += (new \app\admin\model\customer\Service())
                ->where($where)
                ->sum('sales_commission');
        }

        return $commission;
    }

}
